<template>
  <div class="authorWork" ref="authorbox" @scroll="addData" v-if="data[0]">
    <div class="top-name">
      <div :class="[{ header: isActive }, 'NoBackground']">
        <div class="contentBox">
          <div class="arrowleft" @click="back()">
            <span
              ><van-icon name="arrow-left" size="30" color="#CDCDCD"
            /></span>
          </div>
          <div style="font-size: 18px">
            {{ data[0].author.user_name }}
          </div>
        </div>
      </div>
    </div>
    <!-- 作者作品页 -->
    <div class="top">
      <div class="img-top">
        <img :src="data[0].author.web_url" alt="" />
        <div>{{ data[0].author.user_name }}</div>
      </div>
      <div class="desc">{{ data[0].author.desc }}</div>
      <div class="totals">
        <div class="tos">
          <div>关注</div>
          <div>{{ data[0].author.fans_total }}关注</div>
        </div>
      </div>
    </div>
    <div class="works" v-for="item in data" :key="item.id">
      <!-- 阅读和问答文章 -->
      <div
        class="category13"
        v-if="item.category === '1' || item.category === '3'"
        @click="
          viewArticles(
            item.item_id,
            item.category,
            item.tag_list,
            item.share_list.wx.title
          )
        "
      >
        <div class="head">-{{ changeHead(item.category) }}-</div>
        <div class="title">
          <div>{{ item.share_list.qq.title }}</div>
          <div class="cate">
            <span v-if="item.category === '1'">{{
              changeTitle(item.category) + data[0].author.user_name
            }}</span>
            <span v-if="item.category === '3'">{{
              data[0].author.user_name + changeTitle(item.category)
            }}</span>
          </div>
        </div>
        <div class="info">
          {{ item.share_info.content }}
        </div>
        <img :src="item.share_info.image" alt="" />
        <div class="iconbt">
          <van-icon name="like-o" size="24" /><span
            style="font-size: 12px; color: #000"
            >{{ item.like_count }}</span
          >
        </div>
      </div>
      <!-- 收音机文章 -->
      <div class="category8" v-if="item.category === '8'">
        <div class="titles" v-if="item.id" @click="goRadio(item.item_id)">
          <div class="radioTop">
            <img :src="item.img_url" alt="" />
            <div class="radiomin">
              <div>
                <span>ONE</span>
                <img src="@/assets/硬件_收音机1_1.png" alt="" /><span
                  style="font-size: 12px; margin-left: 10px"
                  >{{ item.volume }}</span
                >
              </div>
            </div>
            <div class="radTit">
              {{ item.title }}
            </div>
          </div>
          <div class="radioBottom">
            <div class="botleft" v-if="item.author">
              <div class="botimg">
                <img :src="item.author.web_url" alt="" /><span>{{
                  item.author.user_name
                }}</span>
              </div>
            </div>
            <div class="botRight">
              <van-icon name="like-o" /><span
                style="font-size: 12px; margin-right: 20px"
                >{{ item.like_count }}</span
              >
              <img src="@/assets/分享.png" alt="" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getWorks } from "@/api/articlesPage";
export default {
  name: "AuthorWork",
  data() {
    return {
      data: [],

      page: 0,
      isActive: false,
      obj: { 1: "阅读", 3: "问答", 8: "ONE收音机" },
      // loading: true,
    };
  },
  created() {
    this.getWorkData();
  },
  computed: {
    changeHead() {
      return (i) => {
        return this.obj[i];
      };
    },
    changeTitle() {
      return (i) => {
        if (i === "1") {
          return "文/";
        } else if (i === "3") {
          return "答";
        }
      };
    },
  },
  methods: {
    back() {
      this.$router.go(-1);
    },
    async getWorkData() {
      const { data } = await getWorks(this.$route.query.id, this.page);
      this.data = data;
      // this.loading = false;
      console.log("作者作品", data);
    },
    addData() {
      //scrollTop就是触发滚轮事件时滚轮的高度
      // let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      let scrollTop = this.$refs.authorbox.scrollTop;
      let scrollHeight = this.$refs.authorbox.scrollHeight; //滚动条的高度
      let clientHeight = this.$refs.authorbox.clientHeight; //浏览器的可视高度
      console.log("scrollTop", scrollTop);
      console.log(
        "滚动条高度",
        scrollHeight,
        "浏览器可视高度",
        scrollTop + clientHeight + 100
      );
      if (scrollTop >= 60) {
        this.isActive = true;
      } else {
        this.isActive = false;
      }
      if (scrollTop + clientHeight >= scrollHeight) {
        if (this.timer) {
          clearTimeout(this.timer);
        }
        this.timer = setTimeout(() => {
          this.getLoading();
        }, 1000);
        console.log("触底");
      }
    },
    async getLoading() {
      this.page = this.page + 1;
      const { data } = await getWorks(this.$route.query.id, this.page);
      this.data.push(...data);
      console.log("加载作者作品", data);
    },
    viewArticles(id, category, title, titleTR) {
      // 跳转到文章详情页
      if (category === "1") {
        console.log("跳转的id", id);
        this.$router.push({
          path: "/articlesPage",
          query: {
            id: id,
            category: category,
            title: JSON.stringify(title),
            //  title:  encodeURIComponent(title),
            titleTR: encodeURIComponent(titleTR),
          },
        });
      } else if (category === "3") {
        this.$router.push({
          path: "/answer",
          query: {
            id: id,
            category: category,
            title: JSON.stringify(title),
            //  title:  encodeURIComponent(title),
            titleTR: encodeURIComponent(titleTR),
          },
        });
      }
    },

    goRadio(id) {
      // 跳转到收音机页面
      console.log("跳转到收音机页面");
      this.$router.push({
        path: "/radio",
        query: {
          id: id,
        },
      });
    },
    // 处理字数长度的方法
    getLength(str) {
      if (str.length > 15) {
        return str.substr(0, 18) + "...";
      } else {
        return str;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.authorWork {
  width: 375px;
  background: #f7f7f7;
  overflow: scroll;
  max-height: 667px;
  .top-name {
    .header {
      position: fixed;
      top: 0;
      width: 100%;
      height: 40px;
      text-align: center;
      line-height: 40px;
      background-color: #ffffff;
      z-index: 1;
      color: black;
    }

    .NoBackground {
      position: fixed;
      top: 0;
      width: 100%;
      height: 40px;
      text-align: center;
      line-height: 40px;
      z-index: 1;
      color: black;
    }

    .contentBox {
      position: relative;
      .arrowleft {
        position: absolute;
        top: 5px;
        left: 10px;
      }
    }
  }
}
.top {
  padding: 20px 40px;
  background: #fff;
  .img-top {
    text-align: center;
    font-size: 24px;
    color: #000;
    img {
      width: 80px;
      height: 80px;
      border-radius: 50%;
    }
  }
  .desc {
    font-size: 18px;
    color: #000;
    margin: 20px 0;
  }
  .totals {
    display: flex;
    justify-content: center;
    text-align: center;
    .tos {
      & > :nth-child(1) {
        width: 80px;
        height: 40px;
        border: 1px solid #000;
        line-height: 40px;
        font-size: 18px;
        color: #000;
        border-radius: 5px;
      }
      & > :nth-child(2) {
        margin-top: 10px;
        font-size: 14px;
        color: #c8c8c8;
      }
    }
  }
}
.works {
  margin: 15px 0;
  padding: 15px;
  background: #fff;
  .category13 {
    .head {
      text-align: center;
      font-size: 16px;
      color: #bcbcbc;
    }
    .title {
      margin: 20px 0;
      & > :nth-child(1) {
        font-size: 24px;
        color: #000;
        font-weight: 800;
      }
      .cate {
        padding-top: 10px;
        font-size: 16px;
        color: #bcbcbc;
      }
    }
    .info {
      font-size: 18px;
      color: #808080;
    }
    img {
      width: 100%;
      border-radius: 10px;
      margin-top: 10px;
      margin-bottom: 20px;
    }
    .iconbt {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      span {
        margin-left: 5px;
      }
    }
  }
  .category8 {
    .titles {
      .radioTop {
        position: relative;
        img {
          width: 100%;
          display: block;
          border-radius: 10px;
        }
        .radiomin {
          position: absolute;
          top: 10px;
          left: 10px;
          color: #ffffff;
          & > :nth-child(1) {
            display: flex;
            align-items: center;
            img {
              width: 18px;
              height: 18px;
            }
          }
        }
        .radTit {
          position: absolute;
          left: 10px;
          bottom: 10px;
          color: #ffffff;
          font-size: 14px;
        }
      }
      .radioBottom {
        padding: 10px;
        display: flex;
        justify-content: space-between;

        .botleft {
          .botimg {
            display: flex;
            align-items: center;
            img {
              width: 20px;
              height: 20px;
              border-radius: 50%;
            }
            span {
              font-size: 14px;
              color: #bdbdbd;
              margin-left: 5px;
            }
          }
        }
        .botRight {
          font-size: 20px;
          display: flex;
          align-items: center;
          color: #a7a7a7;
          img {
            width: 20px;
            height: 20px;
          }
        }
      }
    }
  }
}
</style>